//
//  STTimerPickerView.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/13.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STTimerPickerView.h"

#define PickerHeight 180
#define PickerToolBarHeight 44

@implementation STTimerPickerView


-(void)setTimerPickerViewWithArr:(NSArray *)timers withHolder:(NSDictionary *)pholderDic{
    showIndexs = [NSMutableArray arrayWithArray:timers];
    if (_isShowBackView) {
        [self loadBackView];
    }
    [self loadPickerView:pholderDic];
}

-(void)loadBackView{
    backView = [[UIView alloc]initWithFrame:self.bounds];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.5;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [backView addGestureRecognizer:tap];
    [self addSubview:backView];
}

-(void)loadPickerView:(NSDictionary *)holderDic{
    if(!pickerContainerView){
        
        pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - (PickerHeight + PickerToolBarHeight), [UIScreen mainScreen].bounds.size.width, PickerHeight + PickerToolBarHeight)];
        
        UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, PickerToolBarHeight)];
        toolBar.barTintColor = [UIColor whiteColor];
        
        UIBarButtonItem *noSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        noSpace.width=10;
        
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];;
        doneBtn.tintColor = [UIColor grayColor];
        
        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
        cancelBtn.tintColor = [UIColor grayColor];
        
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [toolBar setItems:[NSArray arrayWithObjects:noSpace,cancelBtn,flexSpace,doneBtn,noSpace, nil]];
        UILabel *titleLabel = [UILabel new];
        titleLabel.frame = CGRectMake(0, 0, 200, PickerToolBarHeight);
        titleLabel.center = toolBar.center;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor darkTextColor];
        titleLabel.text = @"服务时间";
        [toolBar addSubview:titleLabel];
        
        pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, PickerToolBarHeight, [UIScreen mainScreen].bounds.size.width, PickerHeight)];
        pickerView.dataSource = self;
        pickerView.delegate = self;
        pickerView.backgroundColor = [UIColor whiteColor];
        resultArr = [NSMutableArray arrayWithCapacity:showIndexs.count];
        for (NSInteger index = 0; index < [showIndexs count]; index ++) {
            [resultArr addObject:@"00"];
            //        [self.pickerView selectRow:index inComponent:index animated:NO];
        }
        UILabel *label1 = [UILabel new];
        label1.frame = CGRectMake(0, 0, 200, 20);
        label1.center =pickerView.center;
        label1.textAlignment = NSTextAlignmentCenter;
        label1.textColor = [UIColor darkTextColor];
        label1.text = @"至";
        UILabel *label2 = [UILabel new];
        label2.frame = CGRectMake(0, 0, 200, 20);
        float w = SCREEN_WIDTH/4;
        label2.center = CGPointMake(w, pickerView.center.y);
        label2.textAlignment = NSTextAlignmentCenter;
        label2.textColor = [UIColor darkTextColor];
        label2.text = @":";
        
        UILabel *label3 = [UILabel new];
        label3.frame = CGRectMake(0, 0, 200, 20);
        float w3 = SCREEN_WIDTH/4*3;
        label3.center = CGPointMake(w3, pickerView.center.y);
        label3.textAlignment = NSTextAlignmentCenter;
        label3.textColor = [UIColor darkTextColor];
        label3.text = @":";
        
        [pickerContainerView addSubview:toolBar];
        [pickerContainerView addSubview:pickerView];
        [pickerContainerView addSubview:label1];
        [pickerContainerView addSubview:label2];
        [pickerContainerView addSubview:label3];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancel:)];
        [self addGestureRecognizer:tap];
        
        [self addSubview:pickerContainerView];
    }
    
    int fromHour =[holderDic[@"fromHour"] intValue];
    int fromMinute =[holderDic[@"fromMinute"] intValue];
    int endHour =[holderDic[@"endHour"] intValue];
    int endMinute =[holderDic[@"endMinute"] intValue];
    [resultArr replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%02d",fromHour]];
    [resultArr replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%02d",fromMinute]];
    [resultArr replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"%02d",endHour]];
    [resultArr replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"%02d",endMinute]];
    [pickerView selectRow:fromHour inComponent:0 animated:NO];
    [pickerView selectRow:fromMinute inComponent:1 animated:NO];
    [pickerView selectRow:endHour inComponent:2 animated:NO];
    [pickerView selectRow:endMinute inComponent:3 animated:NO];
    
//    pickerContainerView
}


#pragma mark-- UIPickerViewDataSource
//列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return [showIndexs count];
}

//每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray * arr =[showIndexs objectAtIndex:component];
    return [arr count];
}


#pragma mark-- UIPickerViewDelegate
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (id)view;
    if (!label)
    {
        label= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        NSString * name = [[showIndexs objectAtIndex:component] objectAtIndex:row];
        label.text = name;
    }
    [pickerView.subviews objectAtIndex:1].backgroundColor = [UIColor orangeColor];
    [pickerView.subviews objectAtIndex:2].backgroundColor = [UIColor lightGrayColor];
    return label;
}

//点击选择
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    [resultArr replaceObjectAtIndex:component withObject:[NSString stringWithFormat:@"%02d",(int)row]];
    
}

#pragma mark--确定
-(void)done:(id)sender{
    
    if (self.selectedItemBlock) {
        self.selectedItemBlock(resultArr);
    }
    [self hide];
}

#pragma mark--取消
-(void)cancel:(id)sender{
    
    [self hide];
}

-(void)hide{
    [backView removeFromSuperview];
    [self removeFromSuperview];
    if (self.cancelItemBlock) {
        self.cancelItemBlock();
    }
//    [self removeFromSuperview];
}



@end
